import {
  listOrder
} from "../../../api/pm";
import {
  getDicts
} from "../../../api/common";
import {
  cancelOrder,
  confirmOrder
} from "../../../api/pm"

const {
  eventBus
} = getApp().globalData;
const app = getApp();
const globalData = app.globalData;
const {
  config,
  colorOption,
  windowInfo,
  curDept
} = app.globalData;
Page({
  data: {
    isData: false,
    pageNum: 1,
    rows: null,
    dict: null
  },
  goBack() {
    wx.navigateBack()
  },
  onLoad() {

    this.gettype();

    this.listOrder();
    //    console.log(this.data.dict)
    //       this.data.rows=this.data.rows.forEach(item=>{
    // item.orderStatus = "111";
    // })  

  },

  listOrder() {
    wx.showLoading({
      title: '加载中',
    });
    this.gettype();
    listOrder({
      data: {
        "pageNum": this.data.pageNum,
        "pageSize": 5
      },
      callBack: (res) => {
        let oldData
        if (!this.data.rows) {
          oldData = []
        } else {
          oldData = this.data.rows
        }
        console.log(oldData)

        //         oldData=oldData.forEach(item=>{
        //   item.orderStatus = "111"; 
        // })  


        let newData = oldData.concat(res.rows.map(item => ({
          ...item, // 复制当前对象的所有属性
          // goodsPic: `config.localImgSrc${item.goodsPic}`, // 修改goodsPic属性，加上新的URL前缀
          goodsPic: config.localImgSrc.concat(item.goodsPic), // 修改goodsPic属性，加上新的URL前缀
          orderStatus: this.data.dict[item.orderStatus],
          // orderStatus :this.data.dict[3],
        })))




        // console.log(newData)

        // console.log(res)
        // console.log(config.localImgSrc)
        // const url=config.localImgSrc
        // console.log(url)



        this.setData({
          rows: newData
        })
        if (this.data.rows.length == res.total || res.rows.length < 5) {
          this.setData({
            isData: true
          })
        }
        wx.stopPullDownRefresh();
        wx.hideLoading();




        // this.setData({
        //   rows: res.rows
        // })
        // console.log(this.data.rows.length)
      }
    })

  },
  onReachBottom() {

    if (this.data.isData) return;
    console.log('我触底了');
    this.data.pageNum = this.data.pageNum + 1;
    this.listOrder();

  },
  gettype() {
    getDicts({
      data: {
        key: 'pm_order_status'
      },
      callBack: res => {
        // console.log(res)

        const statusMap = res.data.reduce((acc, item) => {
          acc[item.dictValue] = item.dictLabel;
          return acc;
        }, {});
        this.setData({
          dict: statusMap,

        })

        // console.log(this.data.dict)
      }
    })
  },
  //确认收货
  confirmOrder(e) {
    console.log(e.currentTarget.dataset.index);
    confirmOrder(e.currentTarget.dataset.index, (res) => {
      console.log(res)
      if (res.code == 200) {

        this.setData({
          isData: false,
          pageNum: 1,
          rows: null,
        })
        this.listOrder();
        wx.showToast({
          title: '收货成功',
          duration: 500,
        })
      }

    })
  },
  //取消订单
  cancelOrder(e) {
    console.log(e.currentTarget.dataset.index);
    cancelOrder(e.currentTarget.dataset.index, (res) => {
      console.log(res.code)
      if (res.code == 200) {
        this.setData({
          isData: false,
          pageNum: 1,
          rows: null,
        })
        this.listOrder();
        wx.showToast({
          title: '取消成功',
          duration: 500,
        })
      }
    })
  }

});